package com.ruoyi.security.elevator.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.security.domain.*;
import com.ruoyi.system.api.enums.ElevatorValue;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

public class ElevatorInterfaceUtil {

    public static String getTimeStamp(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
        String timeStamp = df.format(new Date());
        return timeStamp;
    }


    public static ElevatorResult getMaintenanceOrderInfo( String token, String page,
                                                         String rows, String startUpkeepDate,
                                                         String endUpkeepDate, String factoryNo){
        if (StringUtils.isBlank(page) || StringUtils.isBlank(rows)){
            System.out.println("===> 参数page和rows不能为空");
            return null;
        }
        TreeMap<String, Object> params = new TreeMap<>();
        params.put("appkey", ElevatorValue.APP_KEY);
        params.put("token", token);
        params.put("timeStamp", getTimeStamp());
        params.put("method", "GetMaintenanceOrderInfo");
        params.put("startUpkeepDate", startUpkeepDate);
        params.put("endUpkeepDate", endUpkeepDate);
        params.put("factoryNo", factoryNo);
        params.put("appSecret", ElevatorValue.APP_SECRET);
        params.put("sign", Signed.getSignString(params));
        String request = JSONUtil.toJsonStr(params);
        String resultStr = HttpRequest.post(ElevatorValue.WEBAPI_URL).header("content-type","application/json;charset=utf-8").body(request).execute().body();
        ElevatorResult result  = JSON.parseObject(resultStr, new TypeReference<ElevatorResult<ElevatorInfoList<ElevatorRepairOrder>>>(){});
        return result;
    }

    //获取电梯维保信息
    public static String getElevatorUpkeepInfo( String token, String page,
                                                          String rows, String startUpkeepDate,
                                                          String endUpkeepDate, String factoryNo){
        if (StringUtils.isBlank(page) || StringUtils.isBlank(rows)){
            System.out.println("===> 参数page和rows不能为空");
            return null;
        }
        TreeMap<String, Object> params = new TreeMap<>();
        params.put("appkey", ElevatorValue.APP_KEY);
        params.put("appSecret", ElevatorValue.APP_SECRET);
        params.put("token", token);
        params.put("timeStamp", getTimeStamp());
        params.put("method", "GetElevatorUpkeepInfo");
        // userName
        params.put("userName",ElevatorValue.USER);
        params.put("password",Md5Util.generate32md5(ElevatorValue.PASSWORD));
        params.put("page", page);
        params.put("rows", rows);
        params.put("sign", Signed.getSignString(params));
        String request = JSONUtil.toJsonStr(params);
        String resultStr = HttpRequest.post(ElevatorValue.WEBAPI_URL).header("content-type","application/json;charset=utf-8").body(request).execute().body();
        //ElevatorResult result  = JSON.parseObject(resultStr, new TypeReference<ElevatorResult<ElevatorInfoList<ElevatorRepairOrder>>>(){});
        return resultStr;
    }

    public static ElevatorResult<ElevatorInfoList> getRepairList( String token, String page,
                                                     String rows, String startUpkeepDate,
                                                     String endUpkeepDate, String factoryNo){
        if (StringUtils.isBlank(page) || StringUtils.isBlank(rows)){
            System.out.println("===> 参数page和rows不能为空");
            return null;
        }

        TreeMap<String, Object> params = new TreeMap<>();
//        params.put("appkey", ElevatorValue.APP_KEY);
//        params.put("token", token);
//        params.put("appSecret", ElevatorValue.APP_SECRET);
//        params.put("timeStamp", getTimeStamp());
//        params.put("method", "GetRepairOrderInfo");
//        params.put("page", page);
//        //password
//        params.put("password",Md5Util.generate32md5(ElevatorValue.PASSWORD));
//        params.put("rows", rows);
//        params.put("startUpkeepDate", startUpkeepDate);
//        params.put("endUpkeepDate", endUpkeepDate);
//        params.put("factoryNo", factoryNo);
//        // userName
//        params.put("userName",ElevatorValue.USER);
        params.put("appkey", ElevatorValue.APP_KEY);
        params.put("appSecret", ElevatorValue.APP_SECRET);
        params.put("method", "GetRepairOrderInfo");
        params.put("page", page);
        //password
        params.put("password",Md5Util.generate32md5(ElevatorValue.PASSWORD));
        params.put("rows", rows);

        params.put("timeStamp", getTimeStamp());
        params.put("token", token);
        // userName
        params.put("userName",ElevatorValue.USER);

        params.put("sign", Signed.getSignString(params));
        String request = JSONUtil.toJsonStr(params);
        String resultStr = HttpRequest.post(ElevatorValue.WEBAPI_URL).header("content-type","application/json;charset=utf-8").body(request).execute().body();
        ElevatorResult result  = JSON.parseObject(resultStr, new TypeReference<ElevatorResult<ElevatorInfoList<AfElevatorRepair>>>(){});
        return result;

    }


    /**
     * 获取电梯统计信息
     * @param appkey
     * @param token
     * @param userName
     * @param password
     * @return
     */
    public static ElevatorResult<ElevatorStatistical> getStatisticalInfo( String token){
        TreeMap<String, Object> params = new TreeMap<>();
        params.put("appkey", ElevatorValue.APP_KEY);
        params.put("token", token);
        params.put("appSecret", ElevatorValue.APP_SECRET);
        params.put("timeStamp", getTimeStamp());
        params.put("method", "GetElevatorStatusInfo");
        params.put("sign", Signed.getSignString(params));
        String request = JSONUtil.toJsonStr(params);
        String resultStr = HttpRequest.post(ElevatorValue.WEBAPI_URL).header("content-type","application/json;charset=utf-8").body(request).execute().body();
        ElevatorResult result  = JSON.parseObject(resultStr, new TypeReference<ElevatorResult<ElevatorStatistical>>(){});
        return result;

    }


    /**
     * 获取电梯基础信息
     * @param page
     * @param rows
     * @param appkey
     * @param token
     * @param params
     * @return
     */
    public static ElevatorResult<ElevatorInfoList<ElevatorBaseInfo>> getElevatorInfoList(String page,String rows,String token,TreeMap<String, Object> params){
        if (StringUtils.isBlank(page) || StringUtils.isBlank(rows)){
            System.out.println("===> 参数page和rows不能为空");
            return null;
        }
        if (params == null){
            params = new TreeMap<>();
        }

        params.put("appkey", ElevatorValue.APP_KEY);
        params.put("token", token);
        params.put("timeStamp", getTimeStamp());
        params.put("method", "GetElevatorBasicInfo");
        params.put("page", page);
        params.put("rows", rows);
        params.put("appSecret", ElevatorValue.APP_SECRET);
        params.put("sign", Signed.getSignString(params));

        String request = JSONUtil.toJsonStr(params);
        String resultStr = HttpRequest.post(ElevatorValue.WEBAPI_URL).header("content-type","application/json;charset=utf-8").body(request).execute().body();
        ElevatorResult result  = JSON.parseObject(resultStr, new TypeReference<ElevatorResult<ElevatorInfoList<ElevatorBaseInfo>>>(){});

        return result;
    }

    /**
     * 获取电梯维保单列表
     * @param page
     * @param rows
     * @param appkey
     * @param token
     * @param params
     * @return
     */
    public static ElevatorResult<ElevatorInfoList<AfElevatorMaintenance>> getMaintenanceOrderInfoList(String page,String rows,String token,TreeMap<String, Object> params){
        if (StringUtils.isBlank(page) || StringUtils.isBlank(rows)){
            System.out.println("===> 参数page和rows不能为空");
            return null;
        }
        if (params == null){
            params = new TreeMap<>();
        }

        params.put("appkey", ElevatorValue.APP_KEY);
        params.put("appSecret", ElevatorValue.APP_SECRET);
        params.put("method", "GetMaintenanceOrderInfo");
        params.put("page", page);
        //password
        params.put("password",Md5Util.generate32md5(ElevatorValue.PASSWORD));
        params.put("rows", rows);

        params.put("timeStamp", getTimeStamp());
        params.put("token", token);
        // userName
        params.put("userName",ElevatorValue.USER);
        params.put("sign", Signed.getSignString(params));
        String request = JSONUtil.toJsonStr(params);
        String resultStr = HttpRequest.post(ElevatorValue.WEBAPI_URL).header("content-type","application/json;charset=utf-8").body(request).execute().body();
        ElevatorResult result  = JSON.parseObject(resultStr, new TypeReference<ElevatorResult<ElevatorInfoList<AfElevatorMaintenance>>>(){});

        return result;
    }


    /**
     * 获取电梯维保单列表
     * @param page
     * @param rows
     * @param appkey
     * @param token
     * @param params
     * @return
     */
    public static ElevatorResult1<AfElevatorHistory> getHistoryRunningDataList(String[] factoryNO,String queryDate,String token,TreeMap<String, Object> params){
        if (params == null){
            params = new TreeMap<>();
        }

        params.put("appkey", ElevatorValue.APP_KEY);
        params.put("appSecret", ElevatorValue.APP_SECRET);
        params.put("method", "GetHistoryRunningData");
        //password
        params.put("password",Md5Util.generate32md5(ElevatorValue.PASSWORD));
        if(StringUtils.isNotEmpty(factoryNO)){
            String tmp = StringUtils.join(factoryNO,",");
            params.put("factoryNO",tmp);
        }
        if(StringUtils.isNotEmpty(queryDate)){
            params.put("queryDate",queryDate);
        }
        params.put("timeStamp", getTimeStamp());
        params.put("token", token);
        // userName
        params.put("userName",ElevatorValue.USER);
        params.put("sign", Signed.getSignString(params));
        String request = JSONUtil.toJsonStr(params);
        String resultStr = HttpRequest.post(ElevatorValue.WEBAPI_URL).header("content-type","application/json;charset=utf-8").body(request).execute().body();
        ElevatorResult1 result  = JSON.parseObject(resultStr, new TypeReference<ElevatorResult1<AfElevatorHistory>>(){});

        return result;
    }

    //test
//    public static void main(String[] args) {
//        String token ="450a1c69-ca78-48d5-a29d-b0f1361cfa49-1";
////        String[] test = {"00022021","22019"};
////        ElevatorResult1<AfElevatorHistory> result = ElevatorInterfaceUtil.getHistoryRunningDataList(test, "2022-07-26",token,null);
////        List<AfElevatorHistory> list = result.getData();
////        for(AfElevatorHistory history:list){
////            System.out.println(history.toString());
////            System.out.println(history.getRunCount());
////        }
//        //ElevatorInterfaceUtil.getMaintenanceOrderInfoList(1+"",50+"",token,null);
//        getElevatorUpkeepInfo(token,1+"",50+"",null,null,null);
//    }

    /**
     * 设置电梯事件回调地址
     * @param url
     * @param token
     * @param appkey
     * @return
     */
    public static ElevatorResult setCallbackUrl(String url, String token){
        TreeMap<String, Object> params = new TreeMap<>();
        params.put("callbackUrl", url);
        params.put("timeStamp", getTimeStamp());
        params.put("appkey", ElevatorValue.APP_KEY);
        params.put("appSecret", ElevatorValue.APP_SECRET);
        params.put("method", "SetCallbackUrl");
        params.put("token", token);
        params.put("sign", Signed.getSignString(params));

        String request = JSONUtil.toJsonStr(params);
        String resultStr = HttpRequest.post(ElevatorValue.WEBAPI_URL).header("content-type","application/json;charset=utf-8").body(request).execute().body();
        ElevatorResult result  = JSON.parseObject(resultStr, ElevatorResult.class);
        return result;

    }



}
